const Base = require('./base.js');
const moment = require('moment');

module.exports = class extends Base {
    /**
     * 获取小程序下单参数
     * @returns {Promise<*>}
     */
    async orderAction() {
        let d = this.post();
        
        let out_trade_no=await this.model('order').where({id: d.order_id}).field('out_trade_no').find();
        
        // if(!!out_trade_no){
        //     d.out_trade_no = out_trade_no;
        // }else{
        //     d.out_trade_no = 'DD' + moment().format('YYYYMMDDHHmmss') + Math.random().toString().substr(2, 7);
        // }
        d.out_trade_no = 'DD' + moment().format('YYYYMMDDHHmmss') + Math.random().toString().substr(2, 7);
        
        
        const fee = await this.model('order').where({id: d.order_id}).field('order_price').find();
        d.order_pirce = fee.order_price;
        // d.order_pirce = 0.01;
        // console.log(d)
        const payment = this.service('payment');
        const res = await payment.xcx_order(d);
        
        //return this.fail(res);
        
        if (res.package === 'prepay_id=undefined') {
            return this.fail('支付失败，请稍候重试！');
        }
        await this.model('order').where({id: d.order_id}).update({out_trade_no: d.out_trade_no});
        return this.success({out_trade_no: d.out_trade_no, payargs: res});
    }

};
